package com.shumi.rbac.config;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.shumi.enums.IReturnEnum;
import com.shumi.fun.WebResult;
import com.shumi.rbac.service.JwtTokenService;

@Component
@Slf4j
public class TokenInterceptor implements HandlerInterceptor {

    WebResult webResult;
    /**
     * 登录session key
     */
    public final static String SESSION_KEY = "user";

    @Autowired
    JwtTokenService tokenService;

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
    }

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object view) throws Exception {

//        log.info("-----preSession:"+request.getSession().getId());
        if(request.getSession() == null) {
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(JSON.toJSONString("{'data':'false'}"));
        }else{
            if (request.getSession().getAttribute("user") == null) {
                response.setCharacterEncoding("UTF-8");
                response.getWriter().write(JSON.toJSONString("{'data':'false'}"));
                return false;
            }
        }

        //已登录的跳转到登录界面


//        webResult=new WebResult();
//        String token = request.getHeader(StaticVarEnum.TOKEN.getKey());
//        if (token == null) {
//            return  response(ReturnEnum.FAILED_3,response);
//        }
//        //校验token是否存在
//        if(!tokenService.getTokenMap().containsKey(token)){
//            return  response(ReturnEnum.FAILED_1,response);
//        }
//        TokenVO tokenVO =tokenService.getTokenMap().get(token);
//        //过期则移除token
//        if(tokenVO.getExpirationDate()<new Date().getTime()){
//            tokenService.destory(token);
//            return  response(ReturnEnum.FAILED_2,response);
//        }
//        //更新过期时间
//        tokenService.updateExpirationDate(token);
//        response.setHeader(StaticVarEnum.TOKEN.getKey(),token);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    boolean response(ReturnEnum returnEnum,HttpServletResponse response) throws IOException {
        log.error(returnEnum.message);
        webResult.setResultMsg(returnEnum.message);
        webResult.setResultCode(returnEnum.code);
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(JSON.toJSONString(webResult));
        return false;
    }


    @Getter
    static  enum  ReturnEnum implements IReturnEnum {
        SUCCESS(11,"token校验成功",true),
        FAILED_1(-11,"token非法",false),
        FAILED_2(-12,"token超时",false),
        FAILED_3(-13,"token 不存在",false),
        ;

        String code;
        String message;
        boolean success;
        Object returnObject;

        ReturnEnum(int code, String message, boolean success) {
            this.code=TokenInterceptor.class.getSimpleName()+"."+code;
            this.message=message;
            this.success=success;
        }
    }

}
